﻿using System;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Intel_Client_GUI
{
	public class SysInfoViewModel: INotifyPropertyChanged
	{
		public SysInfoViewModel()
		{
			// 在此点下面插入创建对象所需的代码。
			db = Globle.DbWrap;
			Globle.DateTimeData.OnTimeChanged+=new Intel_Client_GUI.DateTimeModel.DateTimeChangeHandler(DateTimeData_OnTimeChanged);
            this.CalcTable(Globle.DateTimeData.TimeAndDuration.start, Globle.DateTimeData.TimeAndDuration.duration);
            //this.DateTimeData_OnTimeChanged(Globle.DateTimeData.TimeAndDuration);
		}
		~SysInfoViewModel()
		{
			Globle.DateTimeData.OnTimeChanged-=this.DateTimeData_OnTimeChanged;
		}
		
		public DataTable CpuChartSource
		{
			get{return this.cpuChartSource;}
			set{this.cpuChartSource = value;this.NotifyPropertyChanged("CpuChartSource");}
		}
		private DataTable cpuChartSource = null;
		
		public DataTable MemChartSource
		{
			get{return this.memChartSource;}
			set{this.memChartSource = value;this.NotifyPropertyChanged("MemChartSource");}
		}
		private DataTable memChartSource = null;
		
		public DataTable DiskChartSource
		{
			get{return this.diskChartSource;}
			set{this.diskChartSource = value;this.NotifyPropertyChanged("DiskChartSource");}
		}
		private DataTable diskChartSource = null;
		
		
		
		
		
		
		private void DateTimeData_OnTimeChanged(Intel_Client_GUI.DateTimeModel.TimeInfo info)
		{
			// 在此处添加事件处理程序实现。
			this.CalcTable(info.start,info.duration);
		}
		
		private void CalcTable(DateTime start,TimeSpan duration)
		{
			string SQLCpu = @"select min(time('00:00:00',strftime('%H:%M:00',timestamp))) as t , ceil(avg(cpu)) as cpu from SysPerfRecords where timetick group by strftime('%H',timestamp),ceil(strftime('%M',timestamp)/5)";
			DataTable dtcpu = db.GetQueryTable(SQLCpu,start,duration);
			
			string SQLMem = @"select min(time('00:00:00',strftime('%H:%M:00',timestamp))) as t , ceil(avg(memory)) as mem from SysPerfRecords where timetick group by strftime('%H',timestamp),ceil(strftime('%M',timestamp)/5)";
			DataTable dtmem = db.GetQueryTable(SQLMem,start,duration);
			
			string SQLDisk = @"select strftime('%H:00:00',timestamp) as t , ceil(avg(diskwrite)) as w ,ceil(avg(diskread)) as r from SysPerfRecords where timetick group by t";
			DataTable dtdisk = db.GetQueryTable(SQLDisk,start,duration);
			
			this.CpuChartSource = dtcpu;
			this.MemChartSource = dtmem;
			this.DiskChartSource= dtdisk;
		}
		
		private DataBaseWrap db = null;
		
		
		#region INotifyPropertyChanged
		public event PropertyChangedEventHandler PropertyChanged;

		
		private void NotifyPropertyChanged(String info)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(info));
			}
		}
		#endregion
	}
}